package com.neusoft.neuiotms.dcom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.bean.ProjectUserBean;
import com.neusoft.neuiotms.dcom.dto.InstitutionsDTO;
import com.neusoft.neuiotms.dcom.dto.ResidentChangeLogDTO;
import com.neusoft.neuiotms.dcom.service.InstitutionsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "单位管理")
@RestController
@RequestMapping(value = "/api/w/institutions")
public class InstitutionsController {

    @Autowired
    private InstitutionsService institutionsService;

    @ApiOperation("查询所有单位")
    @GetMapping
    public ResultListDTO<InstitutionsDTO> index(final Searchable searchable) {
        return ResultListDTO.success(this.institutionsService.index(searchable));
    }

    @ApiOperation("分页查询所有单位")
    @GetMapping("/p")
    public PageResultDTO<InstitutionsDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.institutionsService.search(pageable, searchable));
    }

    @ApiOperation("创建单位")
    @PostMapping("")
    public ResultDTO<InstitutionsDTO> create(@RequestBody final InstitutionsDTO dto) {
        return ResultDTO.success(this.institutionsService.create(dto));
    }

    @ApiOperation(value = "修改单位")
    @PutMapping("/{id}")
    public ResultDTO<InstitutionsDTO> update(@PathVariable final Long id,
            @RequestBody final InstitutionsDTO dto) {
        return ResultDTO.success(this.institutionsService.update(id, dto));
    }

    @ApiOperation("单位详情")
    @GetMapping("/{id}")
    public ResultDTO<InstitutionsDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.institutionsService.details(id));
    }

    @ApiOperation("删除单位")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.institutionsService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("查询当前用户所在组的所有成员的单位")
    @GetMapping("/by/user")
    public ResultListDTO<InstitutionsDTO> getInstitutions(final UserBean user) {
        return ResultListDTO.success(this.institutionsService.getPermInstitutions(user));
    }

    @ApiOperation("查询当前用户项目个人信息")
    @GetMapping("/me")
    public ResultDTO<ProjectUserBean> myInstitutions(final UserBean user) {
        return ResultDTO.success(this.institutionsService.getMyInstitutions(user));
    }

    @ApiOperation("单位驻场变更日志")
    @GetMapping("/{id}/logs")
    public ResultListDTO<ResidentChangeLogDTO> logs(@PathVariable final Long id) {
        return ResultListDTO.success(this.institutionsService.getResidentChangeLogs(id));
    }
}
